package cc.linker.steplink.crm.merchanth5.service.redis;

import cc.linker.steplink.crm.commons.utils.StringUtil;
import cc.linker.steplink.crm.merchanth5.dao.MaiBaoLaDao;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Author: CKQ
 * @DateTime: 2020/6/12 11:19
 * @Description: TODO
 */
@Service
@Slf4j
public class CustomerCacheService {

    @Autowired
    private MaiBaoLaDao maiBaoLaDao;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public final String STORE_INFO = "customer_store_info:";

    /**
    * @Description：缓存用户店铺信息
    * @DateTime：2020/12/1 17:23
    * @Params：
    * @Return：
    */
    public Map<String, Object> getCustomerCacheStoreInfo(String orgId, String customerId, String type) {
        String storeInfoStr = this.stringRedisTemplate.opsForValue().get(STORE_INFO + customerId);
        if (StringUtils.isEmpty(storeInfoStr)) {
            Map<String, Object> storeInfoMap = maiBaoLaDao.getShopName(orgId, customerId, type);
            this.stringRedisTemplate.opsForValue().set(STORE_INFO + customerId, storeInfoMap == null ? "" : JSON.toJSONString(storeInfoMap));
            return storeInfoMap;
        }
        return (Map<String, Object>) JSON.parse(storeInfoStr);
    }
}
